'''
Created on Jan 18, 2010

@author: christianp
'''
from math import ceil
from math import floor

class Extent(object):

    __minX = 0.0
    __minY = 0.0
    __maxX = 0.0
    __maxY = 0.0

    def __init__(self, min_x, min_y, max_x, max_y): 
        self.__minX = min_x 
        self.__minY = min_y
        self.__maxX = max_x
        self.__maxY = max_y
        
        if (min_x > max_x) or (min_y > max_y):
            raise Exception('Arguments', 'Min should be smaller then max')

    def getCenterX(self):            
        return (self.__minX + self.__maxX) / 2.0
    
    def getCenterY(self):
        return (self.__minY + self.__maxY) / 2.0
    
    def getWidth(self):
        return self.__maxX - self.__minX
    
    def getHeight(self):
        return self.__maxY - self.__minY
    
    def getArea(self):
        return self.getWidth() * self.getHeight() 
    
    def intersectsWith(self, other):
        return Extent(max([self.__minX, other.__minX]),
                      max([self.__minY, other.__minY]),
                      min([self.__maxX, other.__maxX]),
                      min([self.__maxY, other.__maxY]))
    
    def isValid(self):
        if (self.getMinX() == 0):
            if (self.getMinY() == 0):
                if (self.getMaxX() == 0.1):
                    if (self.getMaxY() == 0.1):
                        return False
        
        return True
    
    
    def isIntersecting(self, box):
        if (box.__minX > self.__maxX):
            return False
        elif(box.__maxX < self.__minX):
            return False
        elif(box.__minY > self.__maxY):
            return False
        elif(box.__maxY < self.__minY):
            return False
        return True
        
    def getPosInExtent(self, pos):
        pos.x = min(pos.x, self.__maxX)
        pos.x = max(pos.x, self.__minX)
        pos.y = min(pos.y, self.__maxY)
        pos.y = max(pos.y, self.__minY)
        return pos
    
    def equals(self, extent):
        if(self.__minX != extent.__minX):
            return False
        if(self.__minY != extent.__minY):
            return False
        if(self.__maxX != extent.__maxX):
            return False
        if(self.__maxY != extent.__maxY):
            return False
        return True;
        
    def getMinX(self):
        return self.__minX
    
    def getMinY(self):
        return self.__minY
    
    def getMaxX(self):
        return self.__maxX
    
    def getMaxY(self):
        return self.__maxY
    
    def setMinX(self, min_x):
        self.__minX = min_x
    
    def setMinY(self, min_y):
        self.__minY = min_y
    
    def setMaxX(self, max_x):
        self.__maxX = max_x

    def setMaxY(self, max_y):
        self.__maxY = max_y
        
    def _print(self, prefix):
        print(prefix + " min_x = " + str(self.__minX))
        print(prefix + " min_y = " + str(self.__minY))
        print(prefix + " max_x = " + str(self.__maxX))
        print(prefix + " max_y = " + str(self.__maxY))